<?php
class Aapply {
	public static function main($funcode) {
		sview::verify(true, true);
		switch ($funcode) {
			case 1:
				self::__doapply();
				break;
			case 2:
				self::__dosubmit();
				break;
			default:
				sview::$view->assign_error(404, lang::text(404));
		}
	}
	
	//主播申请历史记录 5-5-1
	private static function __doapply() {
		$acl    = acl::getinst('vedio');
		$userid = $acl->user['id'];
		$user   = model::load('live/userinfo')
			->set_devision($userid)->get_one($userid);
		$where  = array('userid'=>$userid);
		$data   = model::load('live/applications')->get_item($where);
		if (!$data) {
			$data = model::load('live/applications')->get_fields();
			if ($user['email']) {
				$data['email'] = $user['email'];
			}
			$data['birthday'] = strtotime($data['birthday']);
		}
		sview::$view->assign('datasets', $data);
		sview::$view->display();
	}
	
	//提交主播申请表单 5-5-2
	private static function __dosubmit() {
		$acl       = acl::getinst('vedio');
		$userid    = $acl->user['id'];
		$where     = array('userid'=>$userid);
		$item      = model::load('live/applications')->get_item($where);
		$ndate     = date('Ym', config::$nowtime);
		$sdate     = date('Ym', $item['addtime']);
		if ($item && ($item['status'] == 1 || ($item['locker'] >= 3 && $sdate == $ndate))) {
			sview::$view->assign_error(80069, lang::text(80069));
		}
		$data  = array('userid'=>$userid, 'subip'=>get_ip(), 'addtime'=>config::$nowtime, 
			'status'=>0, 'locker'=>1, 'stime'=>config::$nowtime);
		$data['firstname'] = trim(input::get_post('firstname'));
		$data['lastname']  = trim(input::get_post('lastname'));
		$data['gender']    = (int)input::get_post('gender');
		$data['nation']    = trim(input::get_post('nation'));
		$data['talent']    = trim(input::get_post('talent'));
		$data['fbsns']     = trim(input::get_post('fbsns'));
		$data['heardus']   = trim(input::get_post('heardus'));
		$data['birthday']  = trim(input::get_post('birthday'));
		$data['birthday']  = $data['birthday'] ? date('Y-m-d', $data['birthday']) : '';
		$data['email']     = trim(input::get_post('email'));
		$data['phone']     = trim(input::get_post('phone'));
		$data['avtar']     = trim(input::get_post('avtar'));
		//验证屏蔽关键词
		$lang = lang::load('filter.php');
		if ($data['firstname']) {
			$res = api_im::filter(2, $data['firstname']);
			if ($res && $res['ResultContent'] == 1) {
				$view->assign_error(80081, lang::replace(80088, $lang['firstname']));
			}
		}
		if ($data['lastname']) {
			$res = api_im::filter(2, $data['lastname']);
			if ($res && $res['ResultContent'] == 1) {
				$view->assign_error(80082, lang::replace(80088, $lang['lastname']));
			}
		}
		if ($data['nation']) {
			$res = api_im::filter(2, $data['nation']);
			if ($res && $res['ResultContent'] == 1) {
				$view->assign_error(80083, lang::replace(80088, $lang['location']));
			}
		}
		if ($data['talent']) {
			$res = api_im::filter(2, $data['talent']);
			if ($res && $res['ResultContent'] == 1) {
				$view->assign_error(80084, lang::replace(80088, $lang['talent']));
			}
		}
		if ($data['heardus']) {
			$res = api_im::filter(2, $data['heardus']);
			if ($res && $res['ResultContent'] == 1) {
				$view->assign_error(80085, lang::replace(80088, $lang['heardus']));
			}
		}
		if (!verify::is_email($data['email'])) {
			$view->assign_error(80009, lang::text(80009));
		}
		if (!$avtar) {
			unset($data['avtar']);
		}
		if ($item) {
			$data['locker'] = (date('Ym', $item['addtime']) == $ndate)? ($item['locker']+1):1;
			model::load('live/applications')->save($item['id'], $data);
		} else {
			model::load('live/applications')->new_one($data);
		}
		sview::$view->display();
	}
}